package com.guguskins.back.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guguskins.back.common.exception.GlobalMsgException;
import com.guguskins.back.entity.GuBox;
import com.guguskins.back.entity.GuCategory;
import com.guguskins.back.entity.GuGoods;
import com.guguskins.back.entity.bo.SaveCategoryBo;
import com.guguskins.back.entity.vo.PageVo;
import com.guguskins.back.mapper.GuCategoryMapper;
import com.guguskins.back.service.GuCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;

/**
 * <p>
 * 分类 服务实现类
 * </p>
 *
 * @author mybatis-plus
 * @since 2021-05-14
 */
@Service
public class GuCategoryServiceImpl extends ServiceImpl<GuCategoryMapper, GuCategory> implements GuCategoryService {

    @Autowired
    private GuCategoryMapper categoryMapper;

    @Override
    public PageVo<GuCategory> backPage(int p, int size) {
        IPage<GuCategory> iPage = new Page<>(p, size);
        iPage = this.page(iPage);
        return new PageVo<>(iPage.getRecords(), iPage.getTotal());
    }


    @Override
    public List<GuCategory> indexData() {
        List<GuCategory> categories = categoryMapper.indexData();
        categories.forEach(category -> {
            List<GuBox> boxes = category.getBoxes();
            boxes.forEach(box -> {
                List<GuGoods> goodsList = box.getGoodsList();
                GuGoods goods = goodsList.stream().max(Comparator.comparing(GuGoods::getGoodsPrice)).orElse(new GuGoods());
                box.setGoodsIconUrl(goods.getGoodsIconUrl());
                box.setGoodsList(null);
            });
        });
        return categories;
    }

    @Override
    public boolean backAdd(SaveCategoryBo saveCategoryBo) {
        int count = this.count(new LambdaQueryWrapper<GuCategory>() {
            {
                eq(GuCategory::getCategoryName, saveCategoryBo.getCategoryName());
            }
        });
        if (count > 0) {
            throw new GlobalMsgException("分类已存在");
        }
        GuCategory category = new GuCategory();
        category.setCategoryName(saveCategoryBo.getCategoryName());
        category.setCategoryState(saveCategoryBo.getCategoryState());
        category.setCategorySort(saveCategoryBo.getCategorySort());
        return category.insert();
    }

    @Override
    public boolean backEdit(SaveCategoryBo saveCategoryBo) {
        int count = this.count(new LambdaQueryWrapper<GuCategory>() {
            {
                eq(GuCategory::getCategoryName, saveCategoryBo.getCategoryName());
            }
        });
        if (count > 0) {
            throw new GlobalMsgException("分类已存在");
        }
        GuCategory category = new GuCategory();
        category.setCategoryId(saveCategoryBo.getCategoryId());
        category.setCategoryName(saveCategoryBo.getCategoryName());
        category.setCategoryState(saveCategoryBo.getCategoryState());
        category.setCategorySort(saveCategoryBo.getCategorySort());
        return category.updateById();
    }
}
